<hr>
<p>title: 1995.count-special-quadruplets<br>date: 2021-12-07 10:52:55<br>mathjax: true<br>tags:</p>
<pre><code>- LeeCode
</code></pre><p>categories: </p>
<pre><code>- LeeCode
</code></pre><p>hidden: true</p>
<h2 id="cateHidden-false"><a href="#cateHidden-false" class="headerlink" title="cateHidden: false"></a>cateHidden: false</h2><h3 id="描述"><a href="#描述" class="headerlink" title="描述"></a>描述</h3><blockquote>
<p>Given a 0-indexed integer array nums, return the number of distinct quadruplets (a, b, c, d) such that:</p>
<ul>
<li>nums[a] + nums[b] + nums[c] == nums[d], and</li>
<li>a &lt; b &lt; c &lt; d</li>
</ul>
</blockquote>
<h3 id="测试用例"><a href="#测试用例" class="headerlink" title="测试用例"></a>测试用例</h3><pre><code class="lang-bash">Input: nums = [1,1,1,3,5]
Output: 4
Explanation: The 4 quadruplets that satisfy the requirement are:
- (0, 1, 2, 3): 1 + 1 + 1 == 3
- (0, 1, 3, 4): 1 + 1 + 3 == 5
- (0, 2, 3, 4): 1 + 1 + 3 == 5
- (1, 2, 3, 4): 1 + 1 + 3 == 5
</code></pre>
<h3 id="题解"><a href="#题解" class="headerlink" title="题解"></a>题解</h3><pre><code class="lang-js">var countQuadruplets = function (nums) {
  if (nums.length &lt; 4) return 0;
  let sum = 0,
    len = nums.length;
  // 后面至少保留3位
  for (let i = 0; i &lt; len - 3; i++) {
    for (let j = i + 1; j &lt; len - 2; j++) {
      for (let k = j + 1; k &lt; len - 1; k++) {
        let _sum = nums[i] + nums[j] + nums[k];
        let r = len - 1;
        while (k &lt; r) {
          if (_sum === nums[r]) {
            sum++;
          }
          r--;
        }
      }
    }
  }
  return sum;
};
</code></pre>
<h3 id="结果"><a href="#结果" class="headerlink" title="结果"></a>结果</h3><blockquote>
<p>Accepted</p>
<p>211/211 cases passed (436 ms)</p>
<p>Your runtime beats 5.19 % of javascript submissions</p>
<p>Your memory usage beats 5.19 % of javascript submissions (46.4 MB)</p>
</blockquote>
